import {
  getSearch,
  searchData,GoodsSearch,
} from "../../request/api"
Page({
  data: {
    value: "",
    placeholder: "",
    // 区块显示(1-History,2-list,3-goods)
    num: 1,

    defaultKeyword: {},
    // 历史区块
    historyKeywordList: [],
    // 热门区块
    hotKeywordList: [],
    // 搜索出来的数据的列表
    listArr:[],
    // 商品数组
    goodsList:[],
    // 分类数组
    filterCategory:[],
  },
  goSearchFn(v){
    // console.log(v.detail);
    this.setData({value:v.detail},()=>{
      this.onSearch();
    });
  },
  onSearch() {
    // 搜索跳转
    GoodsSearch({
      keyword:this.data.value
    }).then(res=>{
      if(res.errno===0){
        // console.log(res);
      let{goodsList,filterCategory}=res.data;
      this.setData({
        goodsList,
        filterCategory,
        num:3
      })
      }
    })
  },
  // 输入事件
  inputFn(e) {
    // console.log(e.detail);
    this.setData({
      value: e.detail,num:2
    }, () => {
      searchData({
        keyword: this.data.value
      }).then(res => {
        if(res.errno===0){
          //  console.log(res);
          //  显示list组件
          this.setData({listArr:res.data,num:2});
        }
      })
    })

  },
  onCancel() {
    wx.navigateBack()
  },
  onLoad(options) {
    this.getData();
  },
  getData() {
    getSearch().then(res => {
      if (res.errno === 0) {
        let {
          defaultKeyword,
          hotKeywordList,
          historyKeywordList
        } = res.data;
        this.setData({
          defaultKeyword,
          hotKeywordList,
          historyKeywordList,
        })
      }
    })

  },
  // 清除后刷新页面
  refresh() {
    this.getData();
  }
})